<?

	$mapper = Mozg_Mapper::Get('Structure');

	$rootElem = $mapper->GetOne(array('id' => $this->rootId));
	if (!$rootElem) return '';

    $currentObj = $rootElem;
    $subElementsIds = array_keys($mapper->GetList(array('parentId' => $currentObj->GetId())));

	if (sizeof($subElementsIds))
    {
    	?><ul><?php
    }

    for ($i = 0; $i < sizeof($subElementsIds); $i++)
    {
        $id = $subElementsIds[$i];
        $elemObj = $mapper->GetOne(array('id' => $id));

        $isLastElement = $i == (sizeof($subElementsIds) - 1);

        $subElemThis = array_keys($mapper->GetList(array('parentId' => $id)));
        $isSubElements = sizeof($subElemThis);
        $isActiveElement = $this->cid == $id;
        $isInPath = in_array($id, $this->path);
        switch (true) {
            case $isSubElements && $isLastElement && $isInPath:
                $class = 'minus_end';
                $span = '<span><!-- --></span>';
                break;
            case $isSubElements && ! $isLastElement && $isInPath:
                $class = 'minus';
                $span = '<span><!-- --></span>';
                break;
            case $isSubElements && $isLastElement:
                $class = 'plus_end';
                $span = '<span><!-- --></span>';
                break;
            case $isSubElements && ! $isLastElement:
                $class = 'plus';
                $span = '<span><!-- --></span>';
                break;
            case ! $isSubElements && $isLastElement:
                $class = 'end';
                $span = '';
                break;
            case ! $isSubElements && ! $isLastElement:
                $class = '';
                $span = '';
                break;
        }

        $elementClass = array('leaf');
        if ($isActiveElement) $elementClass[] = 'here';

        $elementClass = implode(' ', $elementClass);
        if ($elementClass) $elementClass = ' class="' .$elementClass. '"';

        ?><li <?=($class ? ' class="' . $class . '"' : '');?>>
        	<?=$span;?>
        	<div class="cat-block<?php echo !$elemObj->GetActive() ? ' inactive' : ''; ?>" data-id="<?=$elemObj->GetId();?>">
        		<i class="doc"></i>
        		<a href="<?=htmlspecialchars(Mozg_Router::Get('http')->Create(array('module' => 'front', 'itemId' => $id)));?>" target="_blank" <?=$elementClass;?> title="id:<?php echo $id; ?>"><?=$elemObj->GetName();?></a>
        	</div><?php

        if ($isSubElements && $isInPath)
        {
            $i = -1;
            $currentObj = $elemObj;
            $subElementsIds = $subElemThis;
            ?><ul <?=(! $isInPath ? ' class="hide"' : '');?>><?
            continue;
        }

        ?></li><?

        do {
            $isLevelUpLast = false;
            if ($isLastElement)
            {
                $isLastElement = false;

				if ($currentObj != $rootElem)
				{
					?></ul></li><?php

					$parentId = $currentObj->GetParentId();
	                $subElementsIds = array_keys($mapper->GetList(array('parentId' => $parentId)));
	                $i = array_search($currentObj->GetId(), $subElementsIds);
	                $currentObj = $mapper->GetOne(array('id' => $parentId));

	                if (($i + 1) >= sizeof($subElementsIds))
	                {
	                    $isLevelUpLast = true;
	                    $isLastElement = true;
	                }
				}
            }
        } while ($isLevelUpLast);

    }

	if (sizeof($subElementsIds))
    {
    	?><ul><?php
    }
